package com.by.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.by.yz.R;
import com.by.yz.activity.Famousdoctorguan;
import com.by.yz.activity.FindDoctorResultActivity;
import com.by.yz.activity.diagnosis_doctor;
import com.by.yz.activity.diagnosis_result;
import com.by.yz.loginUtil.Constants;

import java.util.List;
import java.util.Map;

/**
 * Created by pengpeng on 2017/4/10.
 */
public class GridAdapter extends BaseAdapter {

    private Context con;
    private List<Map<String,Object>> list;
    private LayoutInflater inflater;

    public GridAdapter(Context context,List<Map<String,Object>> list){
        this.con = context;
        this.list = list;
        inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(int position) {
        return Integer.parseInt(list.get(position).get("Type").toString());
    }

    @Override
    public int getViewTypeCount() {
        return Constants.TYPENUM_CONTENT;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        if(position>=getCount()){
            return null;
        }
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {

        viewHolder vh = null;
        final Map<String,Object> map = list.get(arg0);
        if(convertView == null){
            int h=100;
            vh = new viewHolder();
            switch(Integer.parseInt(map.get("Type").toString())) {
                case Constants.NUMFIRST:
                    convertView = inflater.inflate(R.layout.adapter_grid_item, null);
                    vh.tv = (TextView) convertView.findViewById(R.id.AdapterGridItemText);
                    vh.iv = (ImageView) convertView.findViewById(R.id.AdapterGridItemImage);
                    vh.iv.setImageResource(Integer.parseInt(map.get("image").toString()));
                    break;

                case Constants.NUMSECONd:
                    convertView = inflater.inflate(R.layout.adapter_grid_bm, null);
                    vh.tv = (TextView) convertView.findViewById(R.id.AdapterGridItemBMText);
                    h=50;
                    break;

                case Constants.NUMTHREE:
                    convertView = inflater.inflate(R.layout.adapter_grid_three, null);
                    vh.tv = (TextView) convertView.findViewById(R.id.AdapterGridItemBMText);
                    h=50;
                    break;

                case Constants.NUMFOURTH:
                    convertView = inflater.inflate(R.layout.adapter_grid_three, null);
                    vh.tv = (TextView) convertView.findViewById(R.id.AdapterGridItemBMText);
                    h=25;
                    break;
            }

            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                    getDipConversionPx(con,h));
            convertView.setLayoutParams(lp);
            convertView.setTag(vh);
        }else{
            vh = (viewHolder) convertView.getTag();
        }
        vh.tv.setText(map.get("text").toString());

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    Intent intent;
                        switch(Integer.parseInt(map.get("Type").toString())) {
                            case Constants.NUMFIRST:
                                intent = new Intent(context, FindDoctorResultActivity.class);
                                intent.putExtra("searchString", map.get("text").toString());
                                context.startActivity(intent);
                                break;

                            case Constants.NUMSECONd:
                                intent = new Intent(context, diagnosis_doctor.class);
                                intent.putExtra("BingMing", map.get("text").toString());
                                context.startActivity(intent);
                                break;

                            case Constants.NUMTHREE:
                                intent = new Intent(context, Famousdoctorguan.class);
                                intent.putExtra("hospital", map.get("yiyuan").toString());
                                intent.putExtra("department", map.get("text").toString());
                                Log.e("医院post = ",map.get("yiyuan").toString());
                                Log.e("科室post = ",map.get("text").toString());
                                context.startActivity(intent);
                                break;

                            case Constants.NUMFOURTH:
                                intent = new Intent(context, diagnosis_result.class);
                                intent.putExtra("ZhengXing",map.get("text").toString());
                                intent.putExtra("BingMing",map.get("bingming").toString());
                                context.startActivity(intent);
                                break;

                        }
                }
            });

        return convertView;
    }

    public class viewHolder{
        public TextView tv;
        public TextView yiyuan;
        public ImageView iv;
    }

    public int getDipConversionPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}